<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 列表（list） | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 列表（list）</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列是 Python 中最基本的数据结构。序列中的每个元素都分配一个数字-它的位置，或索引，第一个索引就是0，第二个索引是1，依此类推。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 有6个序列的内置类型，但最常见的是列表和元组。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列都可以进行的操作包括索引、切片、加、乘、检查成员。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此外，Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是最常用的 Python 的数据类型，它可以作为一个方括号内的逗号分割值出现。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表的数据项不需要具有相同的类型。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是处理一组有序项目的数据结构，即可以在列表中存储一个序列的项目。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表是可变类型的数据。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">49</span>]: list1 = [<span class="string">'python'</span>,<span class="string">'linxu'</span>,<span class="number">1997</span>,<span class="number">2000</span>]</div><div class="line"></div><div class="line">In [<span class="number">50</span>]: list2 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</div><div class="line"></div><div class="line">In [<span class="number">51</span>]: list3 = [<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>];</div><div class="line">In [<span class="number">52</span>]: list4 = [<span class="string">'a'</span>,<span class="number">1</span>,(<span class="number">4</span>,),[<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>]];</div><div class="line"></div><div class="line">In [<span class="number">53</span>]: type(list1)</div><div class="line">Out[<span class="number">53</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">54</span>]: type(list2)</div><div class="line">Out[<span class="number">54</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">55</span>]: type(list3)</div><div class="line">Out[<span class="number">55</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">56</span>]: type(list4)</div><div class="line">Out[<span class="number">56</span>]: list</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个空的列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">57</span>]: list5 = []</div><div class="line"></div><div class="line">In [<span class="number">58</span>]: type(list5)</div><div class="line">Out[<span class="number">58</span>]: list</div><div class="line"></div><div class="line">In [<span class="number">59</span>]: list6 = list()</div><div class="line"></div><div class="line">In [<span class="number">60</span>]: type(list6)</div><div class="line">Out[<span class="number">60</span>]: list</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表中追加值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">61</span>]: list5.append(<span class="string">'hello'</span>)</div><div class="line"></div><div class="line">In [<span class="number">62</span>]: list5</div><div class="line">Out[<span class="number">62</span>]: [<span class="string">'hello'</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改列表中一个值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">63</span>]: list4</div><div class="line">Out[<span class="number">63</span>]: [<span class="string">'a'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]]</div><div class="line"></div><div class="line">In [<span class="number">64</span>]: list4[<span class="number">0</span>] = <span class="string">'b'</span></div><div class="line"></div><div class="line">In [<span class="number">65</span>]: list4</div><div class="line">Out[<span class="number">65</span>]: [<span class="string">'b'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将两个列表变成一个大的列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">66</span>]: list4 + list5</div><div class="line">Out[<span class="number">66</span>]: [<span class="string">'b'</span>, <span class="number">1</span>, (<span class="number">4</span>,), [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>], <span class="string">'hello'</span>]</div></pre></td></tr></table></figure>
<h2 id="访问列表中的值"><a href="#访问列表中的值" class="headerlink" title="访问列表中的值"></a>访问列表中的值</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用下标索引来访问列表中的值，同样也可以使用方括号的形式截取字符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list1 = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line">list2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span> ];</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"list1[0]: "</span>, list1[<span class="number">0</span>]</div><div class="line"><span class="keyword">print</span> <span class="string">"list2[1:5]: "</span>, list2[<span class="number">1</span>:<span class="number">5</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">list[<span class="number">0</span>]: physics</div><div class="line">list2[<span class="number">1</span>:<span class="number">5</span>]: [<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</div></pre></td></tr></table></figure>
<h2 id="更新列表"><a href="#更新列表" class="headerlink" title="更新列表"></a>更新列表</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以对列表的数据项进行修改或更新，也可以使用 append() 方法来添加列表项。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"Value available at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list[<span class="number">2</span>];</div><div class="line">list[<span class="number">2</span>] = <span class="number">2001</span>;</div><div class="line"><span class="keyword">print</span> <span class="string">"New value available at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list[<span class="number">2</span>];</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Value available at index <span class="number">2</span> :</div><div class="line"><span class="number">1997</span></div><div class="line">New value available at index <span class="number">2</span> :</div><div class="line"><span class="number">2001</span></div></pre></td></tr></table></figure>
<h2 id="删除列表元素"><a href="#删除列表元素" class="headerlink" title="删除列表元素"></a>删除列表元素</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用 del 语句来删除列表的元素。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">list1 = [<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>];</div><div class="line"></div><div class="line"><span class="keyword">print</span> list1;</div><div class="line"><span class="keyword">del</span> list1[<span class="number">2</span>];</div><div class="line"><span class="keyword">print</span> <span class="string">"After deleting value at index 2 : "</span></div><div class="line"><span class="keyword">print</span> list1;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例输出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>]</div><div class="line">After deleting value at index <span class="number">2</span> :</div><div class="line">[<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">2000</span>]</div></pre></td></tr></table></figure>
<h2 id="Python-列表脚本操作符"><a href="#Python-列表脚本操作符" class="headerlink" title="Python 列表脚本操作符"></a>Python 列表脚本操作符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;列表对 + 和 <em> 的操作符与字符串相似。+ 号用于组合列表， </em> 号用于重复列表。</p>
<table>
<thead>
<tr>
<th>Python表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>len([1,2,3])</td>
<td>3</td>
<td>长度</td>
</tr>
<tr>
<td>[1,2,3] + [4,5,6]</td>
<td>[1,2,3,4,5,6]</td>
<td>组合</td>
</tr>
<tr>
<td>[‘Hi!’] * 4</td>
<td>[‘Hi!’,’Hi!’,’Hi!’,’Hi!’]</td>
<td>重复</td>
</tr>
<tr>
<td>3 in [1,2,3]</td>
<td>true</td>
<td>元素是否存在于列表中</td>
</tr>
<tr>
<td>for x in [1,2,3]:print x,</td>
<td>1 2 3</td>
<td>迭代</td>
</tr>
</tbody>
</table>
<h2 id="Python-列表截取"><a href="#Python-列表截取" class="headerlink" title="Python 列表截取"></a>Python 列表截取</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的列表截取实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>L = [<span class="string">'Google'</span>, <span class="string">'Runoob'</span>, <span class="string">'Taobao'</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">2</span>]</div><div class="line"><span class="string">'Taobao'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">-2</span>]</div><div class="line"><span class="string">'Runoob'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>L[<span class="number">1</span>:]</div><div class="line">[<span class="string">'Runoob'</span>, <span class="string">'Taobao'</span>]</div><div class="line">&gt;&gt;&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;描述</p>
<table>
<thead>
<tr>
<th>Python 表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>L[2]</td>
<td>‘Taobao’</td>
<td>读取列表中第三个元素</td>
</tr>
<tr>
<td>L[-2]</td>
<td>‘Runoob’</td>
<td>读取列表中倒数第二个元素</td>
</tr>
<tr>
<td>L[1:]</td>
<td>[‘Runoob’,’Taobao’]</td>
<td>从第儿歌元素开始截取列表</td>
</tr>
</tbody>
</table>
<h2 id="Python-列表函数-amp-方法"><a href="#Python-列表函数-amp-方法" class="headerlink" title="Python 列表函数&amp;方法"></a>Python 列表函数&amp;方法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下函数：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>函数</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/1.%20Python%20List%20cmp(" target="_blank" rel="external">cmp(list1,list2)</a>%E6%96%B9%E6%B3%95.md),比较两个列表的元素</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/2.%20Python%20List%20len(" target="_blank" rel="external">len(list)</a>%E6%96%B9%E6%B3%95.md),列表元素个数</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/3.%20Python%20List%20max(" target="_blank" rel="external">max(list)</a>%E6%96%B9%E6%B3%95.md),返回列表元素最大值</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/4.%20Python%20List%20min(" target="_blank" rel="external">min(list)</a>%E6%96%B9%E6%B3%95.md),返回列表元素最小值</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/python%E5%88%97%E8%A1%A8%E5%87%BD%E6%95%B0/5.%20Python%20List%20list(" target="_blank" rel="external">list(seq)</a>%E6%96%B9%E6%B3%95.md),将元组转换为列表</td>
</tr>
</tbody>
</table>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下方法</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>方法</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/1.%20Python%20List%20append(" target="_blank" rel="external">list.append(obj)</a>%E6%96%B9%E6%B3%95.md),在列表末尾添加新的对象</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/2.%20Python%20List%20count(" target="_blank" rel="external">list.count(obj)</a>%E6%96%B9%E6%B3%95.md),统计某个元素在列表中出现的次数</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/3.%20Python%20List%20extend(" target="_blank" rel="external">list.extend(seq)</a>%E6%96%B9%E6%B3%95.md),在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/4.%20Python%20List%20index(" target="_blank" rel="external">list.index(obj)</a>%E6%96%B9%E6%B3%95.md),从列表中找出某个值第一个匹配项的索引位置</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/5.%20Python%20List%20insert(" target="_blank" rel="external">list.insert(index, obj)</a>%E6%96%B9%E6%B3%95.md),将对象插入列表</td>
</tr>
<tr>
<td>6</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/6.%20Python%20List%20pop(" target="_blank" rel="external">list.pop(obj=list[-1])</a>%E6%96%B9%E6%B3%95.md),移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td>
</tr>
<tr>
<td>7</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/7.%20Python%20List%20remove(" target="_blank" rel="external">list.remove(obj)</a>%E6%96%B9%E6%B3%95.md),移除列表中某个值的第一个匹配项</td>
</tr>
<tr>
<td>8</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/8.%20Python%20List%20reverse(" target="_blank" rel="external">list.reverse()</a>%E6%96%B9%E6%B3%95.md),反向列表中元素</td>
</tr>
<tr>
<td>9</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E5%88%97%E8%A1%A8%E6%96%B9%E6%B3%95/9.%20Python%20List%20sort(" target="_blank" rel="external">list.sort([func])</a>%E6%96%B9%E6%B3%95.md),对厡列表进行排序</td>
</tr>
</tbody>
</table>

	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.7 列表/9. Python 的列表(list)/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
